KNNTree
  1. 以下是聚类树分类器的构造方法,Java code

  2. 这个东西是基于实例的学习方法,用的是聚类的方法解决分类的问题

/**
     * Normally should induce the given data, but
     * there is nothing to induce in a nearest neightbour classifier.
     */
    public ClusModel induceSingle(ClusRun cr) throws ClusException, IOException {
        // First make normal decision tree
        ClusNode orig = (ClusNode)getInduce().induceSingleUnpruned(cr);
 
        System.out.println("Calculating Statistical Measures...");
        // Calculate important measures of the trainingdata.
        RowData trainData = (RowData) cr.getTrainingSet();
        KNNStatistics stats = new KNNStatistics(trainData);
        System.out.println("Done.");
 
        //See if normalization and attribute weights are wanted
        double[] weights;
        if(Settings.kNN_attrWeighted.getValue() && Settings.kNN_normalized.getValue()){
            weights = calcWeights(stats,trainData);
        }
        else {
            weights = noWeights(trainData);
        }
 
 
        //Find out which distance to use
        ClusAttrType[] attrs = trainData.getSchema().getDescriptiveAttributes();
        VectorDistance vd;
        String d = Settings.kNN_vectDist.getValue();
        if (d.equals("Manhattan")){
            vd = new ManhattanDistance(attrs,weights);
        }
        else{
            vd = new EuclidianDistance(attrs,weights);
        }
 
 
        // Next transform decision tree into a kNN-tree
        KNNTree tree = KNNTree.makeTree(cr,orig,vd);
        storeDataInTree((RowData) cr.getTrainingSet(),tree);
 
        return tree;
    }
 
下面是另一种形式的归纳方式,注释的意思是“通常应该归纳给定数据,但是这个最近邻分类器没有需要归纳的东西
    /**
     * Normally should induce the given data, but there is nothing to induce in
     * a nearest neightbour classifier.
     */
    public void induce(ClusRun cr) throws ClusException, IOException {
               // First make normal decision tree
          ClusNode orig = (ClusNode) getInduce().induceSingleUnpruned(cr);
 
        // We store the original decision tree
        // for easy comparison of results.
        cr.getModelInfo(ClusModel.ORIGINAL).setModel(orig);
 
        System.out.println("Calculating Statistical Measures...");
        // Calculate important measures of the trainingdata.
        RowData trainData = (RowData) cr.getTrainingSet();
        KNNStatistics stats = new KNNStatistics(trainData);
        System.out.println("Done.");
 
        // See if normalization and attribute weights are wanted
        double[] weights;
        if (Settings.kNN_attrWeighted.getValue()
                && Settings.kNN_normalized.getValue()) {
            weights = calcWeights(stats, trainData);
        } else {
            weights = noWeights(trainData);
        }
 
        // Find out which distance to use
        ClusAttrType[] attrs = trainData.getSchema().getDescriptiveAttributes();
        VectorDistance vd;
        String d = Settings.kNN_vectDist.getValue();
        if (d.equals("Manhattan")) {
            vd = new ManhattanDistance(attrs, weights);
        } else {
            vd = new EuclidianDistance(attrs, weights);
        }
 
        // Next transform decision tree into a kNN-tree
        KNNTree tree = KNNTree.makeTree(cr, orig, vd);
 
        // Store the data into the correct leafs of the tree
        storeDataInTree(trainData, tree);
 
        // Store this new tree
        cr.getModelInfo(ClusModel.ORIGINAL).setModel(tree);
 
        // try to prune the tree
        // (of course) first see if package clus.pruning is wanted
        double vsb = m_Clus.getSettings().getPruneProportion();
        if (vsb > 0.0) {
            // next make a clone of the tree.
            KNNTree pruned = (KNNTree) tree.cloneTree();
            // Prepare a pruner for the tree
            ClusErrorList error_parent = cr.getStatManager().createEvalError();
            RowData pruneset = (RowData) cr.getPruneSet();
            BottomUpPruningVSB pruner = new BottomUpPruningVSB(error_parent,
                    pruneset);
            // prune the cloned tree
            pruner.prune(pruned);
            // Store the pruned tree
            cr.getModelInfo(ClusModel.PRUNED).setModel(pruned);
        }
 
        // Also store a default prediction tree for comparison
        ClusModel defmodel = ClusDecisionTree.induceDefault(cr);
        cr.getModelInfo(ClusModel.DEFAULT).setModel(defmodel);
 
    }
 

豆丁